package com.quasar.hpatient.widget;

import android.app.TimePickerDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.quasar.hpatient.R;
import com.quasar.hpatient.utils.AlarmTime;


/**
 * 药单item
 */
public class Widget_Alarm_Item extends LinearLayout implements View.OnClickListener,WidgetNumber.OnSave {

    @Override
    public void save(WidgetNumber item) {
        getItem();
    }

    public  interface  OnRemove{
        public void remove(Widget_Alarm_Item item);
    }

    public OnRemove getRemove() {
        return remove;
    }

    public void setRemove(OnRemove remove) {
        this.remove = remove;
    }

    private  OnRemove remove;

    public Widget_Alarm_Item(Context context) {
        super(context);

        LayoutInflater.from(context).inflate(R.layout.widget_alarm_item, this);
        title = (TextView) findViewById(R.id.title);
        num = (WidgetNumber) findViewById(R.id.num);
        num.onSave=this;
        time = (TextView) findViewById(R.id.time);
        del = (TextView) findViewById(R.id.del);

        time.setOnClickListener(this);
        del.setOnClickListener(this);
    }

    private TextView title,time;
    private TextView del;
    WidgetNumber num;

    private AlarmTime item;

    public AlarmTime getItem() {
        item.unit=num.getNumber();
        return item;
    }

    public void setItem(AlarmTime item) {
        this.item = item;
        time.setText("" + item.getTime());
        title.setText("" + item.name);
        num.setNumber(item.unit);
    }

    @Override
    public void onClick(View v) {
        if (v == del) {
            if (remove!=null){
                remove.remove(this);
            }
        } else if (v == time) {
            int hour = item.getHour();
            int minute = item.getMinute();
            TimePickerDialog dialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                    item.setTime(hourOfDay,minute);
                    time.setText(""+item.getTime());
                }
            }, hour, minute, true);
            dialog.show();
        }
    }
}
